package com.lili.sort;

import java.util.*;

/**
 * @Auther: 李 力
 * @Date: 2024/8/2
 * @Description: 基数排序:从低位向高位排序
 * @version: 1.0
 */
public class RadixSort {

    public static void RadixSort(String[] arr, int length) {
        List<String>[] buckets = new ArrayList[128];//存0-127对应的字符
        for (int i = 0; i < buckets.length; i++) {
            buckets[i] = new ArrayList<>();
        }
        for (int i = length - 1; i >= 0; i--) {
            for (String s : arr) {
                buckets[s.charAt(i) - '0'].add(s);
            }
            int k = 0;
            for (List<String> bucket : buckets) {
                for (String s : bucket) {
                    arr[k] = s;
                    k++;
                }
                //把原始数组清空
                bucket.clear();
            }
        }
    }
}
